package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.DecorToolbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.AddCommentActivity;
import com.guaju.yueshangLife.activity.CommentGoodsListActivity;
import com.guaju.yueshangLife.bean.MeOrder;
import com.guaju.yueshangLife.bean.MineOrder;
import com.guaju.yueshangLife.bean.OrderDetailBean;
import com.guaju.yueshangLife.databinding.ItemCommentGoodsBinding;
import com.guaju.yueshangLife.widget.dilivery.LineItem;

import org.w3c.dom.Comment;

import java.util.List;

public class CommentGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<OrderDetailBean.DataBean.ItemsBean> list;
    private int status = 1;


    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;
    private int orderId;

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public CommentGoodsAdapter(Context context, List<OrderDetailBean.DataBean.ItemsBean> list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
        ItemCommentGoodsBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_comment_goods, parent, false);
        InnerHolder innerHolder = new InnerHolder(binding.getRoot());
        return innerHolder;

    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        InnerHolder innerHolder = (InnerHolder) holder;
        innerHolder.setContent(list.get(position));
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return list.size();
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v);
    }

    public CommentGoodsAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(CommentGoodsAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    class InnerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ItemCommentGoodsBinding binding;
        int productId;
        String goodsCover = "";

        public InnerHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.getBinding(itemView);

        }

        public void setContent(OrderDetailBean.DataBean.ItemsBean bean) {
            productId = bean.getId();
            OrderDetailBean.DataBean.ItemsBean.DetailBeanX detail = bean.getDetail();
            if (detail != null) {
                OrderDetailBean.DataBean.ItemsBean.DetailBeanX.CoverBean cover = detail.getCover();
                goodsCover = cover.getHost() + cover.getPath();
                binding.sdvInner.setImageURI(detail.getCover().getHost() + detail.getCover().getPath());
                List<String> attribute = detail.getAttribute();
                String info = "";
                for (String str : attribute) {
                    info += str + ";";
                }
                binding.tvInnerAttr.setText(info.substring(0, info.length() - 1));
            binding.tvInnerNum.setText("" + bean.getAmount());
            binding.tvInnerTitle.setText(bean.getDetail().getName());
            binding.tvInnerPrice.setText(bean.getPrice() + "");

            }
            binding.tvToComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, AddCommentActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("goodsId", productId);
                    bundle.putInt("orderId", orderId);
                    bundle.putString("cover", goodsCover);
                    intent.putExtras(bundle);
                    context.startActivity(intent);


                }
            });

        }

        @Override
        public void onClick(View v) {
        }
    }


}
